package com.ithailong.filemanagement.controller;

import com.ithailong.filemanagement.FileApplication;
import com.ithailong.filemanagement.entity.filestore.FilesStore;
import com.ithailong.filemanagement.mappper.FileSoreMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FilesStoreController {
    private static final String UPLOAD_PATH = "D:/uploads";
    private static final String REPLACE = "/fil/lookFile/";
    @Resource
    private FileSoreMapper fileSoreMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd/");
    private static final Logger logger = LoggerFactory.getLogger(FileApplication.class);

    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
//        获取文件名称
        String name = file.getName();
        byte[] bytes = file.getBytes();
//        获取文件大小
        long size = file.getSize();
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String type = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
        String path = "";
//       根据日期作为文件路径
        String format = sdf.format(new Date());
        File folder = new File(UPLOAD_PATH, format);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newName = UUID.randomUUID().toString() + type;
        file.transferTo(new File(folder, newName));
        path = "http://localhost:9999/file/lookFile/" + format + newName;
        FilesStore filesStore = new FilesStore();
        filesStore.setUrl(path);
        filesStore.setType(substring);
        filesStore.setSize((int) size);
        filesStore.setName(file.getOriginalFilename());
        int insert = fileSoreMapper.insert(filesStore);
        inputStream.close();
        return path;
    }

    @GetMapping("/lookFile/**")
    public void lookFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestURI = request.getRequestURI().trim();
        String basePath = requestURI.substring(REPLACE.length());
        String filePath = UPLOAD_PATH + basePath;
        File file = new File(filePath.trim());
        if (!file.exists()) {
            logger.info("文件名称为" + basePath + "的文件不存在");
            return;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        fileInputStream.close();
        outputStream.close();
    }
}
